<?php

namespace DStatistics;

use Carbon\Carbon;
use Illuminate\Support\Facades\Log;

/**
 * 最近的统计
 *
 * @method self where($column, $operator = null, $value = null, $boolean = 'and')
 *
 */
class LasterDay
{
    /**
     * @var Model $model 模型
     */
    private $model;

    private $day;

    public function __construct($model)
    {
        if (is_string($model)) {
            $this->model = new $model;
        } else {
            $this->model = $model;

        }

    }



    public function __call($name, $arguments)
    {
        call_user_func_array([$this->model,$name],$arguments);
        return $this;
    }


    /**
     * 那天的条件
     * @param $day
     * @return int
     */
    public function day($day)
    {
        $filed = $this->model->field;
        $caron = Carbon::now()->subDays($day);
        if ($this->model->times) {
            $time2 = [$caron->startOfDay()->timestamp, $caron->endOfDay()->timestamp];
        } else {
            $time2 = [$caron->startOfDay()->format($this->model->format), $caron->endOfDay()->format($this->model->format)];

        }
        $this->day =$caron->format('Y-m-d');

        $this->model->whereBetween($filed, $time2);
        return $this;
    }

    /**
     * 那天的统计
     * @param $day
     * @return int
     */
    public function dayCount($day)
    {
        $filed = $this->model->field;
        $caron = Carbon::now()->subDays($day);
        if ($this->model->times) {
            $time2 = [$caron->startOfDay()->timestamp, $caron->endOfDay()->timestamp];
        } else {
            $time2 = [$caron->startOfDay()->format($this->model->format), $caron->endOfDay()->format($this->model->format)];

        }
        $this->day =$caron->format('Y-m-d');

        return $this->model->whereBetween($filed, $time2)->count();
    }

    /**
     * 最近几天的统计
     * @param $days
     * @return int
     */
    public function lastCount($days)
    {
        return $this->lastDays($days)->count();
    }

    /**
     * 最近几天
     * @param $days
     * @return $this
     */
    public function lastDays($days)
    {
        $filed = $this->model->field;
        $caron = Carbon::now()->subDays($days);
        if ($this->model->times) {
            $time = $caron->timestamp;
        } else {
            $time = $caron->format($this->model->format);
        }
        $this->day =$caron->format('Y-m-d');

        Log::info("days $days  time $time ");
        $this->model->where($filed, '>', $time);
        return $this;
    }

    /**
     * 进行统计
     * @param $field
     * @return int
     */
    public function count($field = '*')
    {
        return $this->model->count($field);
    }

    /**
     * 最近几天的列表
     * @param $days
     * @return array
     */
    public function dayLists($days, $func = 'count', $field = '*')
    {
        $this->model->newQuery();
        $list = [];
        $dates = [];
        foreach (range(0, $days) as $d) {
            $this->model->newQuery();
            $value = $this->day($d)->$func($field);
            $date = $this->day;
            $list[] = $value;
            $dates[] = $date;
        }
        $this->model->newQuery();
        return [$list,$dates];

    }

}
